import { createRouter, RouteRecordRaw, createWebHashHistory } from 'vue-router'
import layout from '../layout/index.vue'
// import mediaRoutes from './modules/media'
import orderRoutes from './modules/order'
import productRoutes from './modules/product'
import settingRouter from './modules/setting'
import systemRouter from './modules/system'
import nprogress from 'nprogress'

import 'nprogress/nprogress.css'
import { store } from '@/store'
nprogress.configure({})
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/admin/home'
  },
  {
    path: '/admin',
    component: layout,
    meta: { requiresAuth: true },
    children: [
      {
        path: 'home',
        name: 'home',
        meta: { title: '首页' },
        component: () => import('../views/home/index.vue')
      },
      // mediaRoutes,
      orderRoutes,
      productRoutes,
      settingRouter,
      systemRouter

    ]
  },

  {
    path: '/admin/login',
    component: () => import('../views/login/index.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from) => {
  nprogress.start()
  if (to.meta.requiresAuth && !store.state.user) {
    return {
      path: '/login',
      query: {
        redirect: to.fullPath
      }
    }
  }
})

router.afterEach(() => {
  nprogress.done()
})

export default router
